<?php
class Helper_Template
{
	public $file_not_exist;
	
	function __construct()
	{
		$this->file_not_exist = array();
	}
	
	function getfile($tplname, $type="")
	{
//		if( $this->get_smarty_file($tplname, $type) ){				//check whether exist smarty template
//			return true;
//		} elseif( $this->get_self_defined_file($tplname, $type) ){
//			return true;
//		}
		$file = $this->get_self_defined_file($tplname, $type);
		if( empty($file) ){
			$this->show_errors();
		}
		
		return $file;
	}
	
	public function show_errors()
	{
		echo("The following template file(s) cannot be found on server:<br />");
		foreach( $this->file_not_exist as $key=>$val){
			echo "<b>".$val."</b> not exists!<br />";
		}
		die();
	}
	
	private function get_self_defined_file($tplname, $type)
	{
		//die(var_dump($this->array_variable['array_css']));
		$tplfile = WEBROOT;
		$cachefile = WEBROOT;
		if(defined("SITE_RELATIVE")) {
			$tplfile .= SITE_RELATIVE;
			$cachefile .= SITE_RELATIVE;
		}

		$cachefile .= 'cache'.DIRECTORY_SEPARATOR.'template'.DIRECTORY_SEPARATOR.'self_defined'.DIRECTORY_SEPARATOR;
		
		$tplfile .= 'themes'.DIRECTORY_SEPARATOR.SITE_THEME.DIRECTORY_SEPARATOR.'template'.DIRECTORY_SEPARATOR;
		$cachefile .= 'themes'.DIRECTORY_SEPARATOR.SITE_THEME.DIRECTORY_SEPARATOR;
		
		$tplfile_tmp = $tplfile.$this->get_position();
		$cachefile_tmp = $cachefile.$this->get_position();
		
		$helper_File = Helper::instance("File");
		$tplfile_tmp .= $tplname.'.htm';
		
		//die($tplfile);
		if(file_exists($tplfile_tmp)) {
			$helper_File->createFolder($cachefile_tmp);
			return $this->cacheFile($tplname, $tplfile_tmp, $cachefile_tmp);
		}
		$this->file_not_exist[] = $tplfile_tmp;
		
		$tplfile_tmp = $tplfile;
		$cachefile_tmp = $cachefile;
		if( $type != '') {
			$tplfile_tmp .= $type.DIRECTORY_SEPARATOR;
			$cachefile_tmp .= $type.DIRECTORY_SEPARATOR;
		}
		$tplfile_tmp .= $tplname.'.htm';

		if(file_exists($tplfile_tmp)) {
			$helper_File->createFolder($cachefile_tmp);
			return $this->cacheFile($tplname, $tplfile_tmp, $cachefile_tmp);
		}
		$this->file_not_exist[] = $tplfile_tmp;
	}

	public function cacheFile($tplname, $tplfile, $cachefile)
	{
		$file_str	= '';
		
		$cache_file = $cachefile.$tplname.'.php';
		//die($cache_file);
		if(!file_exists($cache_file) || filemtime($tplfile)>filemtime($cache_file))	{
			$file_str = file_get_contents($tplfile);
			//die(var_dump($file_str));
			$parse=1;
			if($parse){
				$s = array(
						"{@", "@}",
						"<!--#", "#-->"
						);
				$e = array(
						"\r\nEOT;\r\necho ", ";\r\nprint <<<EOT\r\n",
						"\nEOT;\n", "\nprint <<<EOT\n"
						);
				//die(print_r($e));
				if(function_exists('str_ireplace')){
					$file_str = str_ireplace($s, $e, $file_str);
				}else{
					$file_str = str_replace($s, $e, $file_str);
				}
				//die(var_dump($file_str));
				$file_str = str_replace("print <<<EOT\n\nEOT;",'',$file_str);
				$file_str = "<?php\r\nprint <<<EOT\r\n".$file_str."\r\nEOT;\n?>";
			}
			//die(var_dump($file_str));
			$this->WriteFile($cache_file, $file_str);
		}
		return $cache_file;
	}

	private function WriteFile($filename, $data, $method="w", $chmod=0664)
	{
		touch($filename);
		$handle = fopen($filename,$method);
		fwrite($handle,$data);
		fclose($handle);
		if($chmod) {
			return @chmod($filename,$chmod);
		} else {
			return true;
		}
	}
	
	private function get_position()
	{
		$position = '';
		
		if(defined("WEB_POSITION") && WEB_POSITION != "" && WEB_POSITION!="front" && $type=="")
		{
			$position .= WEB_POSITION.DIRECTORY_SEPARATOR;
		}
		
		return $position;
	}
	
	private function get_smarty_file($tplname, $type)
	{
		global $smarty;
		
		if( $type != '' ){
			$type .= DIRECTORY_SEPARATOR;
		}
		
		$type .= $this->get_position();
		$tplname = $type.$tplname.'.tpl';
		
		//echo $smarty->template_dir.$tplname;
		if( file_exists($smarty->template_dir.$tplname) ) {
			$smarty->display($tplname);
			return true;
		} else {
			$file_not_exist = array();
			return false;
		}
	}
}
?>